package nio.bytebuffer;

import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Demo01
 *
 * @author chenxin
 * @date 2022/05/14 15:32
 **/
public class Demo01 {

    public static void main(String[] args) throws Exception {
        FileChannel channel = new FileInputStream("src/main/resources/data.txt").getChannel();

        // 准备缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(10);

        while(true){
            // 读取数据
            int len = channel.read(buffer);
            if(len == -1){
                break;
            }

            // 切换到读模式
            buffer.flip();

            //打印数据
            while (buffer.hasRemaining()){
                byte b = buffer.get();
                System.out.println((char)b);
            }

            // 切换为写模式
            buffer.compact();
        }

    }

}